package com.imti.uridemo;

import android.content.Intent;
import android.net.Uri;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.ImageView;
import androidx.annotation.Nullable;
import androidx.appcompat.app.AppCompatActivity;
import android.os.Bundle;

public class MainActivity extends AppCompatActivity
{

    public static final int REQUEST_CODE_CHOOSE_IMAGE = 0xf1;

    private ImageView imageShow;

    @Override
    protected void onCreate(Bundle savedInstanceState)
    {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);

        initData();
    }

    private void initData()
    {
//        Intent it = new Intent(Intent.ACTION_VIEW);
//
//        Uri uri = Uri.parse("tel:10086");
//        it.putExtra("sms_body", "短信内容");
//        it.setDataAndType(uri, "vnd.android.dir/mms-sms");
//        startActivity(it);

        Button btnChooseImage = findViewById(R.id.btn_choose_image);
        imageShow = findViewById(R.id.image_show);

        btnChooseImage.setOnClickListener(new OnClickListener()
        {
            @Override
            public void onClick(View v)
            {
                Intent intent = new Intent(Intent.ACTION_GET_CONTENT);
                intent.addCategory(Intent.CATEGORY_OPENABLE);
                intent.setType("image/*");

                startActivityForResult(Intent.createChooser(intent, "选择图片"),
                    REQUEST_CODE_CHOOSE_IMAGE);
            }
        });
    }
    @Override
    protected void onActivityResult(int requestCode, int resultCode, @Nullable Intent data)
    {
        super.onActivityResult(requestCode, resultCode, data);

        if (RESULT_OK == resultCode)
        {
            switch (requestCode)
            {
                case REQUEST_CODE_CHOOSE_IMAGE:

                    Uri uri = data.getData();
                    imageShow.setImageURI(uri);
                    break;
            }
        }
    }
}